package com.gangquan360.smartadmin.module.contact.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.contact.dao.ContactDao;
import com.gangquan360.smartadmin.module.contact.domain.dto.ContactDTO;
import com.gangquan360.smartadmin.module.contact.domain.dto.ContactQueryDTO;
import com.gangquan360.smartadmin.module.contact.domain.entity.ContactEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 联系人表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-07 15:06:41
 * @since JDK1.8
 */
@Service
public class ContactService {

    @Autowired
    private ContactDao contactDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-07 15:06:41
     */
    public ResponseDTO<PageResultDTO<ContactDTO>> queryByPage(ContactQueryDTO queryDTO) {
        Page<ContactDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<ContactEntity> entities = contactDao.queryByPage(page, queryDTO);
        List<ContactDTO> dtoList = SmartBeanUtil.copyList(entities, ContactDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<ContactDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }
    
    
    /**
     * 
     * @return
     */
    public ResponseDTO<List<ContactDTO>> queryList() {
    	Wrapper<ContactEntity> wrapper = new EntityWrapper<>();
		List<ContactEntity> selectList = contactDao.selectList(wrapper);
		List<ContactDTO> copyList = SmartBeanUtil.copyList(selectList, ContactDTO.class);
		return ResponseDTO.succData(copyList);
    }

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-07 15:06:41
     */
    public ResponseDTO<String> add(ContactDTO addDTO) {
        ContactEntity entity = SmartBeanUtil.copy(addDTO, ContactEntity.class);
        contactDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-07 15:06:41
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(ContactDTO updateDTO) {
        ContactEntity entity = SmartBeanUtil.copy(updateDTO, ContactEntity.class);
        contactDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-07 15:06:41
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        contactDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-07 15:06:41
    */
    public ResponseDTO<ContactDTO> detail(Long id){
        ContactEntity entity = contactDao.selectById(id);
        ContactDTO dto =  SmartBeanUtil.copy(entity,ContactDTO.class);
        return ResponseDTO.succData(dto);
    }
}
